﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using MBSBLC;
using MBSProcessComponent;
using MBSBusinessEntities;
using MBSVO;
namespace MBS
{
    public partial class Ticket : System.Web.UI.Page
    {
        TableRow objRow;
        TableCell objCell;
        Label lblTicket;
        int bookid;
        BLC blc = new BLC();
        BookingBE bookingBE;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bookid = Convert.ToInt32(Request.QueryString["bookID"]);
                 bookingBE = (BookingBE)blc.GetByID(bookid);

                generateTicket(bookingBE);
               
            }
          ClientScript.RegisterStartupScript(this.GetType(), "Print", "javascript:print()", true);
        }
        #region TicketGeneration
        public void generateTicket(BookingBE bookingBE)
        {
           
            try
            {
                MovieBookingSession mbs = (MovieBookingSession)Session["MBS"];
                int CinemaID = mbs.CinemId;
                int MovieID = mbs.MovieId;
                ShowTimeVO showTimeVO = blc.GetShowTimeByID(mbs.ShowTimeId);
                MovieVO movieVO = blc.GetMovieByID(mbs.MovieId);
                MovieScheduleVO movieScheduleVO = blc.GetMovieScheduleByID(showTimeVO.MovieScheduleID);
            //    ShowTimeBE showTimeBE = (ShowTimeBE)blc.getShowTimeBEByID(mbs.ShowTimeId);
                CinemaVO cinemaVO = blc.GetCinemaByID(CinemaID);
                String CinemaName = cinemaVO.CinemaName;
                String MovieName =movieVO.MovieTitle;
            //    MovieScheduleBE movieScheduleBE=blc.GetMovieScheduleByID(showTimeBE.MovieScheduleID);
                String ScreenNumber = Convert.ToString(movieScheduleVO.ScreenNo);
                string showtime=Convert.ToString(showTimeVO.ShowDateTime);
                if(mbs != null)
                {
               // for (int i = 1; i <= 5; i++)
               // {
                   // Panel objPanel = new Panel();
                    Table objTable = new Table();
                    objRow = new TableRow();
                    objCell = new TableCell();
                    lblTicket = new Label();
                    lblTicket.Text = CinemaName;
                    objCell.Controls.Add(lblTicket);
                    objRow.Cells.Add(objCell);
                    objTable.Rows.Add(objRow);
                    lblTicket = new Label();
                    objRow = new TableRow();
                    objCell = new TableCell();
                    lblTicket.Text = "Screen No: "+"<b>"+ScreenNumber+"</b>";
                    objCell.Controls.Add(lblTicket);
                    objRow.Cells.Add(objCell);
                    objTable.Rows.Add(objRow);
                    lblTicket = new Label();
                    objRow = new TableRow();
                    objCell = new TableCell();
                    lblTicket.Style["Font-Family"]="Free 3 of 9";
                    lblTicket.CssClass = "barcodefont";
                    lblTicket.Text = (Request.QueryString["bookID"]);
                    objCell.Controls.Add(lblTicket);
                    objRow.Cells.Add(objCell);
                    objTable.Rows.Add(objRow);
                    lblTicket = new Label();
                    objRow = new TableRow();
                    objCell = new TableCell();
                    lblTicket.Text = "<b>"+MovieName+"</b>";
                    objCell.Controls.Add(lblTicket);
                    objRow.Cells.Add(objCell);
                    objTable.Rows.Add(objRow);
                    lblTicket = new Label();
                    objRow = new TableRow();
                    objCell = new TableCell();
                    lblTicket.Text = "<b>"+showtime+"</b>";
                    objCell.Controls.Add(lblTicket);
                    objRow.Cells.Add(objCell);
                    objTable.Rows.Add(objRow);
                    lblTicket = new Label();
                    objRow = new TableRow();
                    objCell = new TableCell();
                   // lblTicket.Text ="Seat No:" + mbs.SelectedSeatsList.ToArray().ToString() ;
                    lblTicket.Text = "Seat No: " + getSeats(mbs.SelectedSeatsList);
                    //lblTicket.Text = bookingBE.Seatnumbers.ToString();
                    objCell.Controls.Add(lblTicket);
                    objRow.Cells.Add(objCell);
                    objTable.Rows.Add(objRow);
                    lblTicket = new Label();
                    objRow = new TableRow();
                    objCell = new TableCell();
                    lblTicket.Text = "<b>Enjoy the show</b>";
                    objCell.Controls.Add(lblTicket);
                    objRow.Cells.Add(objCell);
                    objTable.Rows.Add(objRow);
                    objRow = new TableRow();
                    objRow.Height=50;
                    objTable.Rows.Add(objRow);
                    objPanel.Controls.Add(objTable);
                    //Page.Controls.Add(pnlTicket);
                   }
                //}


                mbs.SelectedSeatsList = new List<string>();
            }
            catch(Exception objException)
            {
                string exMessage = objException.Message+System.Environment.NewLine+objException.StackTrace;
                string script = "<script type=\"text/javascript\">alert('" + exMessage + "');</script>";
                ClientScript.RegisterStartupScript(this.GetType(),"ticket",script,true);
            }

        }

        private string getSeats(List<string> list)
        {
            string seatNos = "";
            foreach (string seat in list)
            {
                if (string.Equals(seatNos, ""))
                {
                    seatNos = seat;
                }
                else{
                    seatNos = seatNos + "," + seat;
                }
            }
            return seatNos;
        }
        #endregion TicketGeneration
    }
}